# from scipy import ndimage
# from skimage import data, util,io,color
# from matplotlib import pyplot as plt 
# # # img为原始图像
# # img = data.astronaut()[:, :, 0]
# # 读取图像并转换为灰度图像
# img = io.imread(r'c:\Users\HP\Desktop\python\lena.bmp')  
# img = color.rgb2gray(img)
# # 对图像加入椒盐噪声
# noise_img = util.random_noise(img, mode='s&p',seed=None,clip=True)
# # 中值滤波
# n = 3
# new_img = ndimage.median_filter(noise_img, (n, n))
# # 显示图像
# plt.rcParams['font.sans-serif'] = ['SimHei'] 
# plt.rcParams['axes.unicode_minus'] = False
# plt.subplot(1, 3, 1)
# plt.axis('off')
# plt.imshow(img, cmap = 'gray')
# plt.title('原图像')
# plt.subplot(1, 3, 2)
# plt.axis('off')
# plt.imshow(noise_img, cmap = 'gray')
# plt.title('加噪图像')
# plt.subplot(1, 3, 3)
# plt.axis('off')
# plt.imshow(new_img, cmap = 'gray')
# plt.title('中值滤波')
# # plt.savefig('中值滤波结果.tif')
# plt.show()

from scipy import ndimage
from skimage import data, util, io, color
from matplotlib import pyplot as plt

# 读取图像并转换为灰度图像
img = io.imread(r'c:\Users\HP\Desktop\python\lena.bmp')
img = color.rgb2gray(img)

# 添加椒盐噪声，这里设置椒盐噪声的比例为0.05（可根据实际需求调整）
noise_img = util.random_noise(img, mode='s&p', amount=0.05, seed=None, clip=True)

# 定义不同大小的均值滤波器模板
filter_sizes = [(3, 3), (5, 5), (7, 7)]

# 循环使用不同大小的均值滤波器对含噪图像进行滤波
for size in filter_sizes:
    filtered_img = ndimage.uniform_filter(noise_img, size=size)
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.subplot(1, 3, filter_sizes.index(size) + 1)
    plt.axis('off')
    plt.imshow(filtered_img, cmap='gray')
    plt.title(f'{size[0]}*{size[1]}均值滤波')

# plt.savefig('均值滤波去噪结果.tif')
plt.show()

from scipy import ndimage
from skimage import data, util, io, color
from matplotlib import pyplot as plt

# 读取图像并转换为灰度图像
img = io.imread(r'c:\Users\HP\Desktop\python\lena.bmp')
img = color.rgb2gray(img)

# 添加椒盐噪声，这里设置椒盐噪声的比例为0.05（可根据实际需求调整）
noise_img = util.random_noise(img, mode='s&p', amount=0.05, seed=None, clip=True)

# 定义中值滤波器的尺寸列表
filter_sizes = [3, 5, 7]

# 循环使用不同尺寸的中值滤波器对含噪图像进行滤波
for n in filter_sizes:
    filtered_img = ndimage.median_filter(noise_img, (n, n))
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    plt.subplot(1, 3, filter_sizes.index(n) + 1)
    plt.axis('off')
    plt.imshow(filtered_img, cmap='gray')
    plt.title(f'{n}*{n}中值滤波')

# plt.savefig('中值滤波去噪结果.tif')
plt.show()



# import numpy as np
# from scipy import signal
# from skimage import color, io
# from skimage.util import random_noise
# from scipy.ndimage import median_filter
# from matplotlib import pyplot as plt

# # 定义二维灰度图像的空间滤波函数
# def correl2d(img, window):
#     # 使用滤波器实现图像的空间相关
#     s = signal.correlate2d(img, window, mode='same', boundary='fill')
#     # 归一化处理
#     s = (s - s.min()) / (s.max() - s.min()) * 255
#     return s.astype(np.uint8)

# # 添加噪声的函数
# def add_noise(img, noise_type):
#     if noise_type == 'gaussian':
#         return random_noise(img, mode='gaussian', var=0.01)  # 高斯噪声
#     elif noise_type == 'poisson':
#         return random_noise(img, mode='poisson')  # 泊松噪声
#     elif noise_type == 'salt':
#         return random_noise(img, mode='s&p', amount=0.02)  # 盐噪声
#     elif noise_type == 'pepper':
#         return random_noise(img, mode='s&p', amount=0.02)  # 胡椒噪声
#     elif noise_type == 's&p':
#         return random_noise(img, mode='s&p', amount=0.02)  # 椒盐噪声
#     return img

# # 读取图像并转换为灰度图像
# img = io.imread(r'c:\Users\26356\Desktop\python\lena.jpg')  
# img = color.rgb2gray(img)

# # 打印原始图像信息
# print("原始图像数据类型:", img.dtype)
# print("原始图像形状:", img.shape)

# # 盒状滤波模板
# window1 = np.ones((3, 3)) / (3 ** 2)

# # 添加不同类型的噪声
# noisy_img_gaussian = add_noise(img, 'gaussian')
# noisy_img_poisson = add_noise(img, 'poisson')
# noisy_img_salt = add_noise(img, 'salt')
# noisy_img_pepper = add_noise(img, 'pepper')
# noisy_img_sp = add_noise(img, 's&p')

# # 使用中值滤波器去噪
# denoised_img_gaussian = median_filter(noisy_img_gaussian, size=3)
# denoised_img_poisson = median_filter(noisy_img_poisson, size=3)
# denoised_img_salt = median_filter(noisy_img_salt, size=3)
# denoised_img_pepper = median_filter(noisy_img_pepper, size=3)
# denoised_img_sp = median_filter(noisy_img_sp, size=3)

# # 显示图像
# plt.rcParams['font.sans-serif'] = ['SimHei'] 
# plt.rcParams['axes.unicode_minus'] = False

# # 显示高斯噪声
# plt.subplot(3, 3, 1)
# plt.axis('off')
# plt.imshow(img, cmap='gray')
# plt.title('原图像')

# plt.subplot(3, 3, 2)
# plt.axis('off')
# plt.imshow(noisy_img_gaussian, cmap='gray')
# plt.title('加噪 - 高斯')

# plt.subplot(3, 3, 3)
# plt.axis('off')
# plt.imshow(denoised_img_gaussian, cmap='gray')
# plt.title('去噪 - 高斯')

# # 显示泊松噪声
# plt.subplot(3, 3, 4)
# plt.axis('off')
# plt.imshow(noisy_img_poisson, cmap='gray')
# plt.title('加噪 - 泊松')

# plt.subplot(3, 3, 5)
# plt.axis('off')
# plt.imshow(denoised_img_poisson, cmap='gray')
# plt.title('去噪 - 泊松')

# # 显示盐噪声
# plt.subplot(3, 3, 6)
# plt.axis('off')
# plt.imshow(noisy_img_salt, cmap='gray')
# plt.title('加噪 - 盐')

# plt.subplot(3, 3, 7)
# plt.axis('off')
# plt.imshow(denoised_img_salt, cmap='gray')
# plt.title('去噪 - 盐')

# # 显示胡椒噪声
# plt.subplot(3, 3, 8)
# plt.axis('off')
# plt.imshow(noisy_img_pepper, cmap='gray')
# plt.title('加噪 - 胡椒')

# plt.subplot(3, 3, 9)
# plt.axis('off')
# plt.imshow(denoised_img_pepper, cmap='gray')
# plt.title('去噪 - 胡椒')

# plt.show()

